﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SplenetiX
{
    class Camera
    {
        protected float _zoom; // Camera Zoom
        public Matrix _transform; // Matrix Transform
        public Vector2 _pos; // Camera Position
        protected float _rotation; // Camera Rotation
        protected float _coefscroll;
        

        public Camera(float coefScroll)
        {
            _zoom = 1.0f;
            _rotation = 0.0f;
            _pos = Vector2.Zero;
            _coefscroll = coefScroll;
        }

        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // L'image se retourne en cas de zoom negatif
        }

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        public float coeffScroll
        {
            get { return _coefscroll; }
            set { _coefscroll = value; }
        }

        //  Deplacer la camera
        public void Move(Vector2 amount)
        {
            _pos += amount;
        }

        // Position de la camera
        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }

        public Matrix get_transformation(GraphicsDevice graphicsDevice, Personnage joueur, Level level)
        {
            float posX;
            float posY;
            

            if (joueur.Position.X <= graphicsDevice.Viewport.Width / 2)
             posX = 0;              
            else if (joueur.Position.X >= 32 * level.Longueur - graphicsDevice.Viewport.Width / 2)
                posX = -(32 * level.Longueur - graphicsDevice.Viewport.Width);
            else posX = -_pos.X;

            if (joueur.Position.Y <= graphicsDevice.Viewport.Height / 2f)
                posY = 0;
            else if (joueur.Position.Y <= level.Hauteur * 32 - graphicsDevice.Viewport.Height / 2f)
                posY = -_pos.Y - 210;
            else
            {
                posY = -(level.Hauteur * 32 - graphicsDevice.Viewport.Height);
            }
        


            _transform = Matrix.CreateTranslation(new Vector3(posX * this.coeffScroll, posY * this.coeffScroll, 0)) *
                                              Matrix.CreateRotationZ(Rotation) *
                                              Matrix.CreateScale(new Vector3(Zoom, Zoom, 0));
           

            return _transform;
        }
    }
}
